package com.lmc.aop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lmc.common.LogInfo;
import com.lmc.common.R;
import com.lmc.mapper.WalletDetailLogMapper;
import com.lmc.pojo.Order;
import com.lmc.pojo.User;
import com.lmc.pojo.Wallet;
import com.lmc.pojo.WalletDetailLog;
import com.lmc.service.OrderService;
import com.lmc.service.UserService;
import com.lmc.service.WalletService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 目标对象：WalletController
 * 切面类：LogAspect 日志记录
 */
@Aspect
@Component  //交给ComponentScan组件类扫描，放入到容器中
public class LogAspect {
    @Autowired
    UserService userService;

    @Autowired
    WalletDetailLogMapper walletDetailLogMapper;

    @Autowired
    OrderService orderService;

    @Autowired
    WalletService walletService;

    //切入点
    @Pointcut("execution(* com.lmc.controller.*.*(..))")
    public void myPoint() {
    }

    //环绕通知
    @Around("myPoint()")
    public R<String> logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        //假设从浏览器中拿到用户登录的id
        String userId = "147563212344";
        //1.拿到user、order对象
        User user = userService.getById(userId);
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Order::getUserId, userId);
        Order order = orderService.getOne(lqw);

        //1.拿到钱包对象
        LambdaQueryWrapper<Wallet> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Wallet::getUserId, userId);
        Wallet wallet = walletService.getOne(lqw2);

        //2.获取系统操作时间Date date = new Date();  SimpleDateFormat
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));


        //3.获取 @logInfo(describe = "根据主键查询用户")！！！！！！
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        LogInfo logInfo = method.getAnnotation(LogInfo.class);
        String describe = logInfo.describe();
        String modelName = logInfo.modelName();

        //-----调用目标方法-----
        R<String> response = (R<String>) joinPoint.proceed();
        //4.获取目标方法返回的结果：ResultResponse(200，"操作成功"，user) ---> R.success("消费成功！")
        String operatorResult = response.getMsg();
        //最后：创建日志对象，然后 插入日志表
        WalletDetailLog detailLog = new WalletDetailLog();
        detailLog.setId(null);
        detailLog.setUserName(user.getName());
        detailLog.setOrderId(order.getId());
        detailLog.setAmount(detailLog + user.getName() + " : " + ("消费".equals(modelName) ? "+" : "-") + wallet.getAmount().toString());
        detailLog.setDescribe(describe);
        detailLog.setCreateTime(createTime);
        detailLog.setOperatorResult(operatorResult);
        detailLog.setModelName(modelName);
        walletDetailLogMapper.insert(detailLog);

        return response;//环绕通知，返回目标方法对象
    }

}
